package com.luo.utils;

import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @author:
 * @date: 2021/9/18 21:25
 * @description: 生成uuid和日期结合的文件名
 */
@Component
public class RandomFileNameUtil {
    /**
     * description: 带后缀格式的文件名转化

     * @param source: 源文件名
     * @return: java.lang.String
     * @author: LuoZhiKun
     * @date: 2021/9/18 21:33
     */
    public static String generateRandomFileName(String source){
        String[] split = source.split("\\.");
        String suffix = split[split.length-1];
        String uuid = UUID.randomUUID().toString();
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhmmssS");
        String nowStr = "-" + format.format(date);
        String fileName = uuid + nowStr + "." + suffix;
        return fileName;
    }

    public static String generateRandomName(String source){
        String uuid = UUID.randomUUID().toString();
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhmmssS");
        String nowStr = "-" + format.format(date);
        String fileName = uuid + nowStr;
        return fileName;
    }
}
